package cn.tedu.knows.portal.controller;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

//@RestController是针对异步请求的控制器注解
//由它标记的控制器中的方法返回的对象,会转换成json格式响应给axios
//我们现在需要的是根据不同身份,跳转不同页面,所以需要的是跳转页面
//@Controller标记的控制器当中的方法可以返回特定格式字符以实现跳转页面的效果
@Controller
public class HomeController {
    public static final GrantedAuthority STUDENT =
            new SimpleGrantedAuthority("ROLE_STUDENT");
    public static final GrantedAuthority TEACHER =
            new SimpleGrantedAuthority("ROLE_TEACHER");

    //当前控制器方法是登录后访问以决定跳转首页的
    //所以我们设计它的路径为localhost:8080/和localhost:8080/index.html
    @GetMapping(value = {"/", "/index.html"})
    public String index(@AuthenticationPrincipal UserDetails user) {
        //判断用户详情中是否包含讲师身份
        if (user.getAuthorities().contains(TEACHER)) {
            //返回特定格式字符串
            return "redirect:/index_teacher.html";
        }
        else if (user.getAuthorities().contains(STUDENT)) {
            return "redirect:/index_student.html";
        }
        return null;
    }

}
